//
//  LLSettingVC.m
//  LoveLove
//
//  Created by leiyifan on 2019/6/22.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLSettingVC.h"
#import "ClearCacheTool.h"
#import "LLUserManager.h"
#import "LLLoginViewController.h"
#import "AppDelegate.h"
#import "QWebVC.h"

@interface LLSettingVC ()<UINavigationControllerDelegate>

@property (nonatomic,strong) NSArray *titleArr;
@end

@implementation LLSettingVC

-(NSArray *)titleArr{
    if (!_titleArr) {
        _titleArr = @[@[@"清理缓存"],@[@"关于爱好爱",@"用户协议",@"隐私政策",@"帮助与反馈"]];
    }
    return _titleArr;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     self.navigationController.delegate = self;

    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

-(void)addFootView{
    UIView *footView = [[UIView alloc] init];
     footView.backgroundColor = LLCOLORBG;
    footView.frame = CGRectMake(0, 0, LLScreenWidth, 50);
    UIButton *exitLogBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [exitLogBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitLogBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    exitLogBtn.frame = CGRectMake(0, 0, LLScreenWidth, 40);
    [footView addSubview:exitLogBtn];
    exitLogBtn.backgroundColor = [UIColor whiteColor];
    [exitLogBtn addTarget:self action:@selector(exitLogApp) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableFooterView = footView;
}
//退出登录
-(void)exitLogApp{

    [LLUserManager logout];
     AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    LLNavViewController *vc = [[LLNavViewController alloc]initWithRootViewController:LLLoginViewController.new];
    delegate.window.rootViewController = vc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self titleView];
    self.titleLabel.text = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.contentInset = UIEdgeInsetsMake(-LLNavH, 0,LLNavH, 0);
    self.tableView.frame = CGRectMake(0, 0, LLScreenWidth, LLScreenHeight);
    [self addFootView];
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 3;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  40.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UIView *viewHeaderInSection = [[UIView alloc] init];
//    viewHeaderInSection.backgroundColor = LLCOLORBG;
//    return viewHeaderInSection;
//}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *viewHeaderInSection = [[UIView alloc] init];
    viewHeaderInSection.backgroundColor = LLCOLORBG;
    return viewHeaderInSection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLMyBodyTableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"LLMyBodyTableViewCell"];
    }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *array = self.titleArr[indexPath.section];
    cell.textLabel.text = array[indexPath.row];
        cell.textLabel.textColor = LLColorRGB(0);
        cell.textLabel.font = LLRegularFont(15);
        cell.textLabel.frame = CGRectMake(40, 0, 200, 44);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
        return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [self removeCrash];
    }else{
//        http://www.haoaih.com/h5/index/archive?id=1 注册协议的H5，id=1是用户协议，id=2是隐私协议，3是关于，4是帮助与反馈

        NSString *urlStr = @"";
        QWebVC *vc = [QWebVC new];

        if (indexPath.row == 0) {

            urlStr = @"http://www.haoaih.com/h5/index/archive?id=3";
        }
        else if (indexPath.row == 1){

            urlStr = @"http://www.haoaih.com/h5/index/archive?id=1";
        }
        else if (indexPath.row == 2){

            urlStr = @"http://www.haoaih.com/h5/index/archive?id=2";
        }

        vc.urlString = urlStr;
        vc.showNavBar = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

/**
 清除缓存
 */
-(void)removeCrash{
    //SDWebImage框架自身计算缓存的实现
    NSString *clearCacheName = [NSString stringWithFormat:@"您成功清理了%@缓存",[ClearCacheTool getCacheSize]];
    
    if ([ClearCacheTool clearCaches]) {
        [self showSVProgressHUDInfoWithsStatus:clearCacheName];
       
        
    }else{
         [self showSVProgressHUDInfoWithsStatus:@"已经没有缓存了"];
      
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
